package org.alex.samples.web;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.alex.samples.model.ExamUserRole;
import org.alex.samples.model.User;
import org.alex.samples.service.IExamUserRoleService;
import org.alex.samples.service.IUserService;
import org.alex.samples.vo.CommonMessageResultVO;
import org.alex.samples.vo.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class LoginController {
	@Autowired 
	IUserService userService;
	@Autowired
	IExamUserRoleService roleService;
	
	@RequestMapping(value="/toindex",method  = RequestMethod.GET)
	public String toindex(){
			return "index/index";
	}
	
	@RequestMapping(value="/tologin",method = RequestMethod.GET)
	public String tologin(){
			return "login/login";
	}
	@RequestMapping(value="login",method = RequestMethod.GET)
	public String login(HttpSession session){
		if(session.getAttribute("user") != null){
			return "redirect:/toindex";
		}else{
			return "forward:/tologin";
		}
	}
	@RequestMapping(value="/logout",method  = RequestMethod.GET)
	public String tologin(HttpSession session){
		if(session.getAttribute("user") != null){
			session.removeAttribute("user");
			System.out.println("退出登录");
		}
		return "redirect:/login";
	}
	
	
	@ResponseBody
	@RequestMapping(value="checkUser")
	public CommonMessageResultVO loginIn(UserVO uservo,HttpSession session) throws Exception{
		CommonMessageResultVO result = new CommonMessageResultVO();
		result.setSuccess(false);
		User user = userService.getUserByName(uservo.getName());
		if(uservo.getPassword() != null){
				Date time = null;
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(new Date());
				calendar.add(calendar.MINUTE, -3);
				time = calendar.getTime();
				System.out.println(time + "-------------" + new Date());
				if(user.getLast_lock_time().before(time)){
					System.out.println("没锁定");
					if(uservo.getPassword().equals(user.getPassword())){
						user.setLogin_fail_count(0);
						userService.editUser(user);
						uservo.setAddress(user.getAddress());
						uservo.setEmail(user.getEmail());
						uservo.setGender(user.getGender());
						uservo.setId(user.getId());
						uservo.setLast_lock_time(user.getLast_lock_time());
						uservo.setTel(user.getTel());
						SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
						uservo.setBirthday(sdf.format(user.getBirthday()));
						List<ExamUserRole> roleList = roleService.findAll();
						for(ExamUserRole examUserRole : roleList){
							if(uservo.getId()==examUserRole.getUserId()){
								uservo.setRole(examUserRole.getRoleId());
							}
						}
						session.setAttribute("user", uservo);
						result.setSuccess(true);
						System.out.println("登录成功");
					}else{
						user.setLogin_fail_count(user.getLogin_fail_count()+1);
						if(user.getLogin_fail_count()>3){
							user.setLogin_fail_count(1);
							userService.editUser(user);
						}
						if(user.getLogin_fail_count()==3){
							user.setLast_lock_time(new Date());
							result.setMessage("用户名已被锁定,请3分钟后重新尝试!");
						}else{
						result.setMessage("登录失败还剩下"+(3-user.getLogin_fail_count())+"次机会");
						}
						userService.editUser(user);
					}
				}else{
					Date lockTime = user.getLast_lock_time();
					calendar.setTime(lockTime);
					calendar.add(calendar.MINUTE, +3);
					Date resultTime = calendar.getTime();
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
					result.setMessage("用户名被锁定,请在此时间之后重新登录:"+sdf.format(resultTime));
				}
			
		}
		return result;
		
	}
}
